import 'dart:math';

import 'package:flutter/material.dart';

class StateLessFulWidget extends StatefulWidget {
  const StateLessFulWidget({super.key});

  @override
  State<StateLessFulWidget> createState() => _StateLessFulWidgetState();
}

class _StateLessFulWidgetState extends State<StateLessFulWidget> {
  List<String> list = [];
  int count = 0;
  @override
  Widget build(BuildContext context) {
    MediaQueryData queryData = MediaQuery.of(context);
    double screenWidth = queryData.size.width;
    double screenHeight = queryData.size.height;

    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('有状态和无状态组件')),
      body: ListView(
        children: [
          Column(
            children: _buildTitles(),
          ),
          SizedBox(height: 40),
          Padding(
              padding: EdgeInsets.all(60),
              child: OutlinedButton(
                  style: ButtonStyle(
                      //backgroundColor:MaterialStateProperty.all(Colors.blue),
                      foregroundColor: MaterialStateProperty.all(Colors.grey),
                      side: MaterialStateProperty.all(
                          const BorderSide(width: 1, color: Colors.grey))),
                  onPressed: () {
                    setState(() {
                      int data = Random().nextInt(100);
                      list.add('随机数 $data');
                    });
                  },
                  child: const Text('新增 view 数据')))
        ],
      ),
    ));
  }

  List<Widget> _buildTitles() {
    return list.map((value) {
      //// 将listData 数组 所有元素封装 并返回数组
      return ListTile(
        title: Text(value),
        leading: CircleAvatar(
            backgroundColor: Color(0xffFDCF09),
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                  "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F9b0e4511-18a2-4964-b29e-fa5d754ee257%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1704899284&t=12f449c7957ac085c422d8dadcc667f0"),
            )),
        trailing: Icon(
          Icons.navigate_next,
          color: Colors.grey,
          textDirection: TextDirection.ltr,
        ),
      );
    }).toList();
  }
}
